package com.yzc.service.impl;

import com.yzc.Dto.roleMenu;
import com.yzc.pojo.RoleBean;
import com.yzc.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class RoleDao {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 获取用户数据
     *
     * @return
     */
    public List<RoleBean> loadRoleList() {
        return roleMapper.loadRoleList();
    }

    public Map save(RoleBean roleBean) {
        Map map = new HashMap();
        try {
            if (roleBean.getName() == null || roleBean.getName().isEmpty() || roleBean.getMemo() == null || roleBean.getMemo().isEmpty()) {
                throw new IllegalArgumentException("角色名称不能为空");
            }else {
                roleMapper.save(roleBean);
                map.put("result", "success");
            }
        } catch (Exception e) {
            map.put("result", "error");
            e.printStackTrace();
        }
        return map;
    }

    public Map deleteInfo(RoleBean roleBean) {
        Map map = new HashMap();
        try {
            roleMapper.deleteInfo(roleBean);
            map.put("result", "success");
        } catch (Exception e) {
            map.put("result", "error");
            e.printStackTrace();
        }
        return map;
    }


    public List<roleMenu>  loadRoleMenus(RoleBean roleBean) {
        return roleMapper.loadRoleMenus(roleBean);
    }

    public Map updateInfo(RoleBean roleBean) {
        Map map = new HashMap();
        try {
            roleMapper.updateInfo(roleBean);
            map.put("result", "success");
        } catch (Exception e) {
            map.put("result", "error");
            throw new RuntimeException(e);
        }
        return map;
    }




    public void saveAuthorizationRoleMenu(RoleBean roleBean) {
        roleMapper.deleteRoleMenu(roleBean);
        roleMapper.saveAuthorizationRoleMenu(roleBean);
    }
}
